﻿using System;
using System.Collections.Generic;
using System.IO;
using System.Linq;
using System.Text;
using System.Threading.Tasks;
using Microsoft.VisualBasic;

namespace Updater.Core
{
    public class FileHelper
    {
    /// <summary>
    /// 获取指定目录下的所有文件（相对路径）
    /// </summary>
    /// <param name="path">指定目录（绝对路径）</param>
    /// <returns></returns>
        public static List<string> GetOffspringFiles(string path)
        {
            List<string> files = new List<string>();
            string[] filedirs = Directory.GetFiles(path, "*", SearchOption.AllDirectories);
            foreach (string item in filedirs)
            {
              files.Add(item.Replace(path,string.Empty));
            }
            return files;
        }
        /// <summary>
        /// 确保指定的目录存在
        /// </summary>
        public static void SureDirExistence(string path)
        {
            if (!Directory.Exists(path))
            {
                Directory.CreateDirectory(path);
            }
        }
        public static UpdateConfigInfo GetUpdateConfigInfo(UpdateConfiguration oldconfig,List<FileUnit> newfiles)
        {
            UpdateConfigInfo updateConfigInfo = new UpdateConfigInfo();
            //检测新增的
            foreach (FileUnit f in newfiles)
            {
                bool isadd=true;
                foreach (FileUnit v in oldconfig.FileList)
                {
                    if (v.FileRelativePath==f.FileRelativePath)
                    {
                        isadd = false;
                        //这里检测修改的
                        if (f.FileSize != v.FileSize || f.LastUpdateTime.ToString() != v.LastUpdateTime.ToString()) 
                        {
                            updateConfigInfo.Change.Add(f);


                        }




                    }
                }
                if (isadd)
                {
                    updateConfigInfo.Add.Add(f);
                }


            }
            //删除的
            foreach (FileUnit f in oldconfig.FileList)
            {
                bool found=false;
                foreach (FileUnit v in newfiles)
                {
                    if (f.FileRelativePath == v.FileRelativePath)
                    {
                        found = true;
                        break;
                    }
                }
                if (!found)
                {
                    updateConfigInfo.Delete.Add(f);
                }

            }




            return updateConfigInfo;
        }


        /// <summary>
        ///拷贝文件夹 
        /// </summary>
        /// <param name="srcdir">源目录（请使用绝对路径）</param>
        /// <param name="desdir">目标目录（请使用绝对路径）</param>
        public static void CopyDirectory(string srcdir, string desdir)
        {
            List<string> files = FileHelper.GetOffspringFiles(srcdir);

            foreach (string file in files)
            {
                //源文件名
                string srcfilename = srcdir + "\\" + file;
                //目标文件名
                string desdirname = desdir + "\\" + file;
                //目标的文件夹名
                string despath = System.IO.Path.GetDirectoryName(desdirname);
                //确保目标的文件夹存在
                FileHelper.SureDirExistence(despath);
                File.Copy(srcfilename, desdirname, true);

            }
        }
    }

    public class UpdateConfigInfo
    {
        /// <summary>
        /// 增加的
        /// </summary>
        public List<FileUnit> Add=new List<FileUnit>();
        /// <summary>
        /// 修改的
        /// </summary>
        public List<FileUnit> Change=new List<FileUnit>();
        /// <summary>
        /// 删除的
        /// </summary>
        public List<FileUnit> Delete=new List<FileUnit>();

    }
    
}
